package com.lglin.common.utils.other;


import com.qiniu.common.QiniuException;
import com.qiniu.http.Response;
import com.qiniu.storage.BucketManager;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.Region;
import com.qiniu.storage.UploadManager;
import com.qiniu.util.Auth;
import org.springframework.web.multipart.MultipartFile;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import static com.lglin.common.constant.Constant.*;

public class QiniuCloudUtil {

    public static void uploadFile(MultipartFile file, String fileName){
        try {
            uploadFile(file.getInputStream(), fileName);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    private static boolean uploadFile(InputStream inputStream, String fileName){
        Response response = null;
        try {
            Auth auth = Auth.create(ACCESS_KEY, SECRET_KEY);
            String upToken = auth.uploadToken(BUCKET_NAME);
            Configuration configuration =  new Configuration(Region.huanan());
            UploadManager uploadManager = new UploadManager(configuration);
            response = uploadManager.put(inputStream, 20000, fileName, upToken, null, null, false);
            return response.statusCode == 200;
        } catch (QiniuException e) {
            e.printStackTrace();
            return false;
        }finally {
            if(response != null) {
                response.close();
            }
        }
    }

    /*
     * @Author: 钟棋
     * @describe: 把网络图片上传到服务器
     * @param url 网络图片的路径
     * @param fileName 文件名
     * @return:
     */
    public  static boolean uploadFile(String url,String fileName){
        InputStream inputStream = getInputStreamByUrl(url);
        return uploadFile(inputStream,fileName);
    }

    public static InputStream getInputStreamByUrl(String urlStr){
        DataInputStream in = null;
        URL url = null;
        HttpURLConnection conn = null;
        try {
            url = new URL(urlStr);
            conn = (HttpURLConnection) url.openConnection();
            in = new DataInputStream(conn.getInputStream());
        } catch (IOException e) {
            System.out.println("url转换输入流失败,错误信息"+e.getMessage());
        }finally {
            if(url != null) {
                url = null;
            }
            if(conn != null){
                conn = null;
            }
        }
        return in;
    }

    public static boolean deleteFile(String fileName){
        Auth auth = Auth.create(ACCESS_KEY, SECRET_KEY);
        Configuration config = new Configuration(Region.huadong());
        BucketManager bucketMgr = new BucketManager(auth, config);
        //指定需要删除的文件，和文件所在的存储空间
        Response delete = null;
        try {
            delete = bucketMgr.delete(BUCKET_NAME, fileName);
            return delete.statusCode == 200;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }finally {
            if(delete != null) {
                delete.close();
            }
        }
    }
}